package codechicken.wirelessredstone.core;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class RedstoneEtherGui extends JFrame
{
	private class RedstoneEtherGuiPanel extends JPanel implements MouseListener
	{

		public void paintComponent(Graphics g)
		{
			super.paintComponent(g);

			Graphics2D graphics2d = (Graphics2D) g;
			drawActiveFrequencies(graphics2d);
			drawNodes(graphics2d);
		}
		
		private void drawActiveFrequencies(Graphics2D graphics2d)
		{
			TreeMap<Integer, Integer> map = RedstoneEther.getLoadedFrequencies();
			graphics2d.drawString("Freqs:", 5, 10);
			int i = 2;
			for(Iterator<Integer> iterator = map.keySet().iterator(); iterator.hasNext();)
			{
				int freq = iterator.next();
				graphics2d.drawString((new StringBuilder()).append(freq).append(": ").append(map.get(freq)).toString(), 5, i * 10);
				i++;
			}
		}

		private void drawNodes(Graphics2D graphics2d)//draw different colour if knocked out
		{
			graphics2d.drawString("Receivers:", 50, 10);
			int y = 2;
			int maxy;
			for(int freq = 1; freq <= RedstoneEther.numfreqs; freq++)
			{
				for(int dimension = 0; dimension < 2; dimension++)
				{
					Collection<BlockCoord> recievernodes = RedstoneEther.getReceiversOnFreq(freq, dimension);
	
					for(Iterator<BlockCoord> iterator = recievernodes.iterator(); iterator.hasNext();)
					{
						BlockCoord rxnode = iterator.next();
						graphics2d.drawString(
								(new StringBuilder())
								.append("[").append(freq).append("] at (")
								.append(rxnode.x).append(",").append(rxnode.y).append(",").append(rxnode.z)
								.append(") in dim ").append(CoreProxy.getActualDimension(dimension))
								.toString(), 50, y * 10);
						y++;
					}
				}
			}

			maxy = y * 10;

			graphics2d.drawString("Transmitters:", 250, 10);
			y = 2;
			for(int freq = 1; freq <= RedstoneEther.numfreqs; freq++)
			{
				for(int dimension = 0; dimension < 2; dimension++)
				{
					Map<BlockCoord, Boolean> transmitternodes = RedstoneEther.getTransmittersOnFreq(freq, dimension);
					for(Iterator<BlockCoord> iterator = transmitternodes.keySet().iterator(); iterator.hasNext();)
					{
						BlockCoord txnode = iterator.next();
						graphics2d.drawString(
								(new StringBuilder())
								.append("[").append(freq).append("] at (")
								.append(txnode.x).append(",").append(txnode.y).append(",").append(txnode.z)
								.append("):[").append(transmitternodes.get(txnode))
								.append("] in dim ").append(CoreProxy.getActualDimension(dimension))
								.toString(), 250, y * 10);
						y++;
					}
				}
			}

			if(y * 10 > maxy)
			{
				maxy = y * 10;
			}

			setPreferredSize(new Dimension(500, maxy));
		}

		public void mouseClicked(MouseEvent mouseevent)
		{
		}

		public void mouseEntered(MouseEvent mouseevent)
		{
		}

		public void mouseExited(MouseEvent mouseevent)
		{
		}

		public void mousePressed(MouseEvent mouseevent)
		{
		}

		public void mouseReleased(MouseEvent mouseevent)
		{
		}

		private static final long serialVersionUID = 1L;

		public RedstoneEtherGuiPanel()
		{
			setPreferredSize(new Dimension(500, 200));
			setBackground(Color.white);
		}
	}

	public RedstoneEtherGui()
	{
		setTitle("Ether GUI");
		setLocationRelativeTo(null);
		RedstoneEtherGuiPanel redstoneetherguipanel = new RedstoneEtherGuiPanel();
		JScrollPane jscrollpane = new JScrollPane(redstoneetherguipanel);
		add(jscrollpane);
		addMouseListener(redstoneetherguipanel);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		pack();
	}
	
	public static void openGui()
	{
		gui = new RedstoneEtherGui();
		gui.setVisible(true);
	}

	public static void closeGui()
	{
		gui.dispose();
		gui = null;
	}

	public static boolean guiOn()
	{
		if(gui == null)
		{
			return false;
		}
		else if(gui.isDisplayable())
		{
			return true;
		}
		else
		{
			gui.dispose();
			gui = null;
			return false;
		}
	}
	
	public static void update()
	{
		if(guiOn())
		{
			gui.repaint();
		}
	}
	
	private static RedstoneEtherGui gui;
	
	private static final long serialVersionUID = 1L;
}
